package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.config.RedisConfig;
import com.woniuxy.domain.Orderr;
import com.woniuxy.exception.MyRuntimeException;
import com.woniuxy.mapper.OrderMapper;
import com.woniuxy.service.OrderService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuyanzu
 * @since 2021-05-30
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orderr> implements OrderService {
    @Autowired
    private RedisConfig redisConfig;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Override
    public Integer insertOrder(Orderr orderr) {
        return this.insertOrder(orderr);
    }

    @Override
    public Integer showDayRecord() {
        return Integer.parseInt((String) redisConfig.opsForValue().get("DayOrderRecord"));
    }

    @Override
    public Integer showMonthRecord(String month) throws Exception {
        String value = (String) redisConfig.opsForValue().get("MonthOrderRecord:"+month);
        if(null==value)
            throw new MyRuntimeException("此时间段无数据");
        return Integer.parseInt(value);
    }

    @Override
    public void MovieRank(Orderr orderr) {

        if (null==redisConfig.opsForZSet().score("MovieRank", orderr.getmName())) {
            redisConfig.opsForZSet().add("MovieRank", orderr.getmName(),1);
        }else{
            Double score = redisConfig.opsForZSet().score("MovieRank", orderr.getmName());
            redisConfig.opsForZSet().add("MovieRank", orderr.getmName(), score+1);
        }
    }

    @Override
    public Set<Object> getMovieRank() {
        Set<Object> movieRank = redisConfig.opsForZSet().reverseRange("MovieRank", 0, -1);
        return movieRank;
    }
    public void orderRocket(Orderr orderr){
        redisConfig.opsForHash().put("OrderRocket", orderr.getoId(), orderr);
        Message<Orderr> message = MessageBuilder.withPayload(orderr).build();
        rocketMQTemplate.syncSend("order:woniu",message,10000,3);
    }
}
